﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using iSufeMVC4.SufeAPI;

namespace iSufeMVC4.Core
{
    public static class EamsService
    {
        /// <summary>
        /// 获取学生内部Id
        /// </summary>
        /// <param name="StuCode"></param>
        /// <returns></returns>
        public static int? getStudentId(string StuCode)
        {
            using (EamsDataContext db = new EamsDataContext())
            {
                int? stdId = db.Eams_Student
                    .Where(p => p.StdCode == StuCode)
                    .Select(p => p.StdId)
                    .FirstOrDefault();
                return stdId;
            }
        }
        /// <summary>
        /// 从学号获取课程信息
        /// </summary>
        /// <param name="StdCode">学号，若不匹配返回null</param>
        /// <param name="CalendarId"></param>
        /// <returns></returns>
        public static StudentCourseTable getStudentCourseTable(string StdCode,int CalendarId)
        {
            int? StdId = Core.EamsService.getStudentId(StdCode);
            return StdId == null ? null : getStudentCourseTable((int)StdId, CalendarId);
        }
        /// <summary>
        /// 如果数据存在会冲突，今后再考虑冲突处理
        /// 把对象这样复制一遍好傻啊！
        /// </summary>
        /// <param name="StdId"></param>
        /// <param name="CalendarId"></param>
        /// <returns></returns>
        public static StudentCourseTable getStudentCourseTable(int StdId, int CalendarId)
        {
            //先请求数据库，再请求HTTP
            StudentCourseTable result = getStudentCourseTableFromDB(StdId, CalendarId);
            if (result==null)
            {
                SufeSerive serive = new SufeSerive();
                result = serive.getStudentCourseTable(StdId, CalendarId);
                //执行存储
                using (EamsDataContext db = new EamsDataContext())
                {
                    Eams_StudentCourseTable table= new Eams_StudentCourseTable();
                    //CourseTable
                    table.StdId = result.StdId;
                    table.CanlendarId = result.CalendarId;
                    table.AdminClassName = result.AdminClassName;
                    table.CalendarName = result.CalendarName;
                    table.LastUpdateTime = DateTime.Now;
                    table.StdCode = result.StdCode;
                    table.StdName = result.StdName;
                    table.TotalCredits = result.TotalCredits;
                    db.Eams_StudentCourseTable.InsertOnSubmit(table);
                    db.SubmitChanges();
                    //act
                    foreach (CourseActivity activity in result.Activity)
                    {
                        Eams_StudentCourseActivity actTable = new Eams_StudentCourseActivity();
                        actTable.CanlendarId = result.CalendarId;
                        actTable.ClassroomId = activity.ClassroomId;
                        actTable.ClassroomName = activity.ClassroomName;
                        actTable.CourseId = activity.CourseId;
                        actTable.CourseName = activity.CourseName;
                        actTable.LastUpdateTime = DateTime.Now;
                        actTable.SeqNo = activity.SeqNo;
                        actTable.StdId = result.StdId;
                        actTable.TeacherId = activity.TeacherId;
                        actTable.TeacherName = activity.TeacherName;
                        actTable.ValidWeeks = activity.ValidWeeks;
                        db.Eams_StudentCourseActivity.InsertOnSubmit(actTable);
                        db.SubmitChanges();
                        //act time
                        foreach (CourseActivityTime time in activity.CourseTime)
                        {
                            Eams_StudentCourseActivityTime dbTime = new Eams_StudentCourseActivityTime();
                            dbTime.ActivityKey = actTable.ActivityKey;
                            dbTime.Index = time.Index;
                            dbTime.WeekDay = time.WeekDay;
                            dbTime.LastUpdateTime = DateTime.Now;
                            db.Eams_StudentCourseActivityTime.InsertOnSubmit(dbTime);
                            db.SubmitChanges();
                        }
                    }
                    //task
                    foreach(CourseTaskBrief brief in result.TaskBrief){
                        Eams_StudentCourseTask taskTable = new Eams_StudentCourseTask();
                        taskTable.CanlendarId = result.CalendarId;
                        taskTable.Comments = brief.Comments;
                        taskTable.CourseCode = brief.CourseCode;
                        taskTable.CourseLangType = brief.CourseLangType;
                        taskTable.CourseName = brief.CourseName;
                        taskTable.CourseType = brief.CourseType;
                        taskTable.Credits = brief.Credits;
                        taskTable.FirstLessonDate = brief.FirstLessonDate;
                        taskTable.IsPublic = brief.IsPublic;
                        taskTable.LastUpdateTime = DateTime.Now;
                        taskTable.SeqNo = brief.SeqNo;
                        taskTable.StdId = result.StdId;
                        taskTable.TaskId = brief.TaskId;
                        taskTable.TeacherName = brief.TeacherName;
                        taskTable.WeekStart = brief.WeekStart;
                        taskTable.WeekUnits = brief.WeekUnits;
                        db.Eams_StudentCourseTask.InsertOnSubmit(taskTable);
                        db.SubmitChanges();
                    }
                }
                return result;
            }
            else
            {
                return result;
            }
        }
        /// <summary>
        /// 把对象这样复制一遍好傻啊！
        /// </summary>
        /// <param name="StdId"></param>
        /// <param name="CalendarId"></param>
        /// <returns></returns>
        public static StudentCourseTable getStudentCourseTableFromDB(int StdId, int CalendarId)
        {
            using (EamsDataContext db = new EamsDataContext())
            {
                var dbResult = db.Eams_StudentCourseTable
                    .Where(p => p.StdId == StdId && p.CanlendarId == CalendarId).FirstOrDefault();
                if (dbResult!=null)
                {
                    StudentCourseTable result=new StudentCourseTable();
                    result.StdId = dbResult.StdId;
                    result.AdminClassName = dbResult.AdminClassName;
                    result.CalendarId = dbResult.CanlendarId;
                    result.CalendarName = dbResult.CalendarName;
                    result.StdCode = dbResult.StdCode;
                    result.StdName = dbResult.StdName;
                    result.TotalCredits = (int)dbResult.TotalCredits;
                    result.TaskBrief=new List<CourseTaskBrief>();
                    result.Activity = new List<CourseActivity>();
                    //task
                    foreach (var dbBrief in dbResult.Eams_StudentCourseTask)
                    {
                        CourseTaskBrief brief = new CourseTaskBrief();
                        brief.Comments = dbBrief.Comments;
                        brief.CourseCode = (int)dbBrief.CourseCode;
                        brief.CourseLangType = dbBrief.CourseLangType;
                        brief.CourseName = dbBrief.CourseName;
                        brief.CourseType = dbBrief.CourseType;
                        brief.Credits = (int)dbBrief.Credits;
                        brief.FirstLessonDate = dbBrief.FirstLessonDate;
                        brief.IsPublic = dbBrief.IsPublic;
                        brief.SeqNo = (int)dbBrief.SeqNo;
                        brief.TaskId = dbBrief.TaskId;
                        brief.TeacherName = dbBrief.TeacherName;
                        brief.WeekStart = (int)dbBrief.WeekStart;
                        brief.WeekUnits = (int)dbBrief.WeekUnits;
                        result.TaskBrief.Add(brief);
                    }
                    //act
                    foreach (var dbActivity in dbResult.Eams_StudentCourseActivity)
                    {
                        CourseActivity act = new CourseActivity();
                        act.ClassroomId = dbActivity.ClassroomId;
                        act.ClassroomName = dbActivity.ClassroomName;
                        act.CourseId = dbActivity.CourseId;
                        act.CourseName = dbActivity.CourseName;
                        act.SeqNo=dbActivity.SeqNo;
                        act.TeacherId=dbActivity.TeacherId;
                        act.TeacherName=dbActivity.TeacherName;
                        act.ValidWeeks=dbActivity.ValidWeeks;
                        act.CourseTime = new List<CourseActivityTime>();
                        //act time
                        foreach (var dbTime in dbActivity.Eams_StudentCourseActivityTime)
                        {
                            act.CourseTime.Add( new CourseActivityTime(dbTime.WeekDay,dbTime.Index));
                        }
                        result.Activity.Add(act);
                    }
                    return result;
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
